/**
 * 
 */
package edu.bu.grocery.backingbean;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import edu.bu.grocery.session.UserSession;
import edu.bu.grocerylist.model.GroceryUser;
import edu.bu.grocerylist.service.IGroceryUserService;

/**
 * Class used to help with UI interactions with the registration page
 * 
 * @author donlanp
 * 
 *         03/27/2014 aks - added security question and answer
 */
public class RegistrationBackingBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3497109064180145046L;

	private UserSession session;

	private String username;
	private String password;
	private String securityQuestion;
	private String securityAnswer;

	private IGroceryUserService userService;
	
	private Pattern emailPattern;
	private final static String EMAIL_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
			+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	
	public RegistrationBackingBean(){
		emailPattern = Pattern.compile(EMAIL_REGEX);
	}
	
	
	/**
	 * Registers the users, validation and all
	 * 
	 * @return
	 */
	public String register() {
		// Validate fields
		if (username == null || username.isEmpty()) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please fill out all fields!"));
			return "";
		}

		if (password == null || password.isEmpty()) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please fill out all fields!"));
			return "";

		}

		if (securityQuestion == null || securityQuestion.isEmpty()) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please fill out all fields!"));
			return "";

		}

		if (securityAnswer == null || securityAnswer.isEmpty()) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please fill out all fields!"));
			return "";

		}
		
		//Validate email address
		Matcher matcher = emailPattern.matcher(username);
		if(!matcher.matches()){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please enter a correct email address!"));
			return "";
			
		}
		
		//Validate password
		boolean foundUpper = false;
		boolean foundLower = false;
		boolean foundNumber = false;
		
		for(Character c : password.toCharArray()){
			if(Character.isUpperCase(c)){
				foundUpper = true;
			}
			if(Character.isLowerCase(c)){
				foundLower = true;
			}
			if(Character.isDigit(c)){
				foundNumber = true;
			}
		}
		
		if(!foundUpper || !foundLower || !foundNumber || password.length() < 8){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Please enter a correct password (must contain upper and lower case and at least 8 characters)!"));
			return "";
			
		}

		GroceryUser tmpUser = userService.getUserSecurityQuestion(username);

		System.out.println(securityQuestion + " :: " + securityAnswer);
		if (tmpUser == null) {
			GroceryUser newUser = userService.registerUser(new GroceryUser(
					username, password, securityQuestion, securityAnswer));
			System.out.println(newUser.getUserId());
			if (newUser.getUserId() == 0) {
				FacesContext.getCurrentInstance().addMessage(null,
						new FacesMessage("Could not create user!"));
				return "";
			} else {
				session.setCurrentUser(newUser);
			}
		} else {
			System.out.println("User with such name is already registered: "
					+ tmpUser.getUserId());
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(
							"User with such name is already registered!"));
			return "";
		}

		// if successful, go to grocery lists page
		return "login";
	}

	/**
	 * @return the session
	 */
	public UserSession getSession() {
		return session;
	}

	/**
	 * @param session
	 *            the session to set
	 */
	public void setSession(UserSession session) {
		this.session = session;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/*
	 * set question
	 */
	public void setSecurityQuestion(String question) {
		this.securityQuestion = question;
	}

	/*
	 * get question
	 */
	public String getSecurityQuestion() {
		return this.securityQuestion;
	}

	/*
	 * set answer
	 */
	public void setSecurityAnswer(String answer) {
		this.securityAnswer = answer;
	}

	/*
	 * get answer
	 */
	public String getSecurityAnswer() {
		return this.securityAnswer;
	}

	/**
	 * @return the userService
	 */
	public IGroceryUserService getUserService() {
		return userService;
	}

	/**
	 * @param userService
	 *            the userService to set
	 */
	public void setUserService(IGroceryUserService userService) {
		this.userService = userService;
	}

}
